package dao;

import dao.imp.IUserDao;
import entity.User;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import util.MysqlConnUtil;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: personnel_management_system
 * @description: user dao
 * @author: Mr.Sun
 * @create: 2020-06-18 20:59
 **/

public class UserDao implements IUserDao {
    private JdbcTemplate template;

    public UserDao() throws Exception {
        template = new JdbcTemplate(MysqlConnUtil.getDataSource());
    }

    @Override
    public boolean insert(User obj) {
        String sql ="INSERT INTO user VALUES(?,?,?)";
        int count = template.update(sql,obj.getUname(),obj.getPassword(),obj.getSalt());
        return count == 1;
    }

    @Override
    public boolean update(User obj) {
        return false;
    }

    @Override
    public boolean delete(User obj) {
        return false;
    }

    public User selectByName(String name) throws InvocationTargetException, IllegalAccessException {
        User user  = new User();
        String sql = "SELECT * from user where uname = ?";
        Map map =  template.queryForMap(sql,name);
        BeanUtils.populate(user,map);
        return user;
    }
}
